const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popupCol: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.sku_code){
      this.setData({
        sku_code: options.sku_code
      })
    }

    qqmapsdk = new QQMapWX({
      key: '25FBZ-XOBW3-CHH3V-YBMKG-IVNWT-FYBRE'
    });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      img_url: app.globalData.base_url + 'images/shopList_01.jpg'
    })
    this.adjustLocation();
  },

  //定位
  adjustLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          show_authorization: false
        })
        that.getShopList();
      },
      fail: function (fail_res) {
        // console.log(fail_res)
        if (fail_res.errMsg == "getLocation:fail auth deny" || fail_res.errMsg == "getLocation:fail:auth denied") {
          // that.showModal();
          that.setData({
            show_authorization: true
          })
        } else {
          util.showError("定位失败");
        }
      }
    })
  },

  //获取门店列表
  getShopList:function(){
    var params = new Object();
    var lat = this.data.latitude;
    var lng = this.data.longitude;
    var sku_code = this.data.sku_code;
    var that = this;
    params.lat = lat;
    params.lng = lng;
    params.sku_code = sku_code;
    util.showLoading("加载中...");

    // 调用接口
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      },
      success: function (res) {
        // console.log(res);
        if (res.status == 0) {
          var city = res.result.address_component.city;
          params.city = city;
          api.POST({
            params: params,
            url: app.globalData.base_url + 'winecabinet/shopAddr',
            success: function (res) {
              wx.hideLoading()
              if (util.isEmpty(res.data.code)) {
                // console.log(res.data.data);
                that.setData({
                  shopList: res.data.data.list,
                  shopInfo: res.data.data.info
                })
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              wx.hideLoading()
              util.showError("网络错误，请检查网络");
            }
          })

        }
      },
      fail: function (res) {
        // console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },
  //t跳转到地图显示
  searchMap: function (e) {
    var index = e.currentTarget.dataset.index;
    var shopList_active = this.data.shopList[index];
    var sku_code = this.data.sku_code;
    wx.setStorageSync('shopList_active', shopList_active)
    wx.navigateTo({
      url: '/map/pages/searchMap/searchMap?sku_code=' +sku_code
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})